home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / mit / snmpd / snmpd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-17  |  5.0 KB  |  258 lines

  1.  
  2. /*
  3.  *    $Header: snmpd.c,v 3.0 91/05/17 16:15:26 jrd Rel $
  4.  *    Author: J. Davin
  5.  *    Copyright 1988, 1989, Massachusetts Institute of Technology
  6.  *    See permission and disclaimer notice in file "notice.h"
  7.  */
  8.  
  9. #include    <notice.h>
  10.  
  11. #include    <sys/types.h>
  12. #include    <sys/socket.h>
  13. #include    <netinet/in.h>
  14.  
  15. #include    <stdio.h>
  16. #include    <netdb.h>
  17. #include    <strings.h>
  18.  
  19. #include    <host.h>
  20.  
  21. #include    <ctypes.h>
  22. #include    <debug.h>
  23. #include    <rdx.h>
  24. #include    <smp.h>
  25. #include    <mis.h>
  26. #include    <miv.h>
  27. #include    <aps.h>
  28. #include    <ap0.h>
  29. #include    <asn.h>
  30. #include    <asl.h>
  31. #include    <avl.h>
  32. #include    <udp.h>
  33. #include    <systm.h>
  34. #include    <kmem.h>
  35. #include    <rte.h>
  36. #include    <icmp.h>
  37. #include    <if.h>
  38. #include    <udps.h>
  39.  
  40. #define        cmdStringSize        (32)
  41. #define        cmdBufferSize        (2048)
  42.  
  43. static    void    cmdInit ()
  44.  
  45. {
  46.     aslInit ();
  47.     asnInit ();
  48.     misInit ();
  49.     avlInit ();
  50.     mixInit ();
  51.     apsInit ();
  52.     ap0Init ();
  53.     smpInit ();
  54.  
  55.     kmemInit ();
  56.     systmInit ();
  57.     rteInit ();
  58.     icmpInit ();
  59.     ifInit ();
  60.     udpsInit ();
  61. }
  62.  
  63. static  CIntfType       usage (s)
  64.  
  65. CCharPtrType            s;
  66.  
  67. {
  68.         fprintf (stderr, "Usage: %s", s);
  69.         fprintf (stderr, " [-h lhost]");
  70.         fprintf (stderr, " [-p lport]");
  71.         fprintf (stderr, " [-c community]");
  72.         fprintf (stderr, "\n");
  73.         return (1);
  74. }
  75.  
  76. static    SmpStatusType    myUpCall (smp, community, req)
  77.  
  78. SmpIdType        smp;
  79. ApsIdType        community;
  80. SmpRequestPtrType    req;
  81.  
  82. {
  83.     smp = smp;
  84.     community = community;
  85.     req = req;
  86.     printf ("Upcall:\n");
  87.     return (errOk);
  88. }
  89.  
  90. int            snmpdCommand (argc, argv)
  91.  
  92. int            argc;
  93. char            **argv;
  94.  
  95. {
  96.     int            s;
  97.     int            salen;
  98.     int            result;
  99.     struct    sockaddr    salocal;
  100.     struct    sockaddr    saremote;
  101.     struct    sockaddr_in    *sin;
  102.     struct    servent        *svp;
  103.  
  104.         u_long                  lhost;
  105.         u_short                 lport;
  106.  
  107.     CByteType        buf [ cmdBufferSize ];
  108.     CBytePtrType        bp;
  109.     SmpIdType        smp;
  110.     SmpSocketType        udp;
  111.     ApsIdType        communityId;
  112.         CCharPtrType            *ap;
  113.         CCharPtrType            cp;
  114.         CBoolType               noerror;
  115.     CUnslType        number;
  116.  
  117.         CCharPtrType            communityString;
  118.         CCharPtrType            lhostString;
  119.         CCharPtrType            lportString;
  120.  
  121.     communityString = (CCharPtrType) 0;
  122.     lhostString = (CCharPtrType) 0;
  123.     lportString = (CCharPtrType) 0;
  124.  
  125.     ap = (CCharPtrType *) argv + 1;
  126.     argc--;
  127.     noerror = TRUE;
  128.     while ((argc != 0) && (**ap == (CCharType) '-') && (noerror)) {
  129.         cp = *ap;
  130.         cp++;
  131.         ap++;
  132.         argc--;
  133.         while ((*cp != (CCharType) 0) && (noerror)) {
  134.             switch (*cp) {
  135.  
  136.             case 'c':
  137.                 argc--;
  138.                 communityString = *ap++;
  139.                 break;
  140.  
  141.             case 'h':
  142.                 argc--;
  143.                 lhostString = *ap++;
  144.                 break;
  145.  
  146.             case 'p':
  147.                 argc--;
  148.                 lportString = *ap++;
  149.                 break;
  150.  
  151.             default:
  152.                 noerror = FALSE;
  153.                 break;
  154.             }
  155.             cp++;
  156.         }
  157.     }
  158.  
  159.     if ((! noerror) || (argc > 0)) {
  160.         return ((int) usage ((CCharPtrType) argv [ 0 ]));
  161.     }
  162.  
  163.     if (lhostString != (CCharPtrType) 0) {
  164.         lhost = (u_long) hostAddress (lhostString);
  165.         if (lhost == (u_long) -1) {
  166.             fprintf (stderr, "%s: Bad foreign host: %s\n",
  167.                 argv [ 0 ], lhostString);
  168.             return (2);
  169.         }
  170.     }
  171.     else {
  172.         lhost = (u_long) 0;
  173.     }
  174.  
  175.     if (lportString != (CCharPtrType) 0) {
  176.                 if (rdxDecodeAny (& number, lportString) < (CIntfType) 0) {
  177.                         fprintf (stderr, "%s: Bad local port: %s\n",
  178.                                 argv [ 0 ], lportString);
  179.                         return (2);
  180.                 }
  181.                 else {
  182.                         lport = htons ((u_short) number);
  183.                 }
  184.         }
  185.         else {
  186.                 svp = getservbyname ("snmp", "udp");
  187.                 if (svp == (struct servent *) 0) {
  188.                         fprintf (stderr, "%s: No such service: %s/%s\n",
  189.                                 argv [ 0 ], "snmp", "udp");
  190.                         return (2);
  191.                 }
  192.                 lport = (u_short) svp->s_port;
  193.         }
  194.  
  195.     if (communityString == (CCharPtrType) 0) {
  196.         communityString = (CCharPtrType) "public";
  197.     }
  198.  
  199.     cmdInit ();
  200.  
  201.     s = socket (AF_INET, SOCK_DGRAM, 0);
  202.     if (s < 0) {
  203.         (void) perror ("socket");
  204.         return (1);
  205.     }
  206.  
  207.     sin = (struct sockaddr_in *) & salocal;
  208.         bzero ((char *) sin, sizeof (salocal));
  209.     sin->sin_family = AF_INET;
  210.     sin->sin_addr.s_addr = lhost;
  211.     sin->sin_port = lport;
  212.  
  213.     result = bind (s, & salocal, sizeof (*sin));
  214.     if (result < 0) {
  215.         (void) perror ("bind");
  216.         return (1);
  217.     }
  218.  
  219.     communityId = apsNew ((ApsNameType) communityString,
  220.         (ApsNameType) "trivial", (ApsGoodiesType) 0);
  221.  
  222.     sin = (struct sockaddr_in *) & saremote;
  223.  
  224.     do {
  225.         salen = sizeof (saremote);
  226.         result = recvfrom (s, (char *) buf, (int) cmdBufferSize,
  227.             (int) 0, & saremote, & salen);
  228.         DEBUG1 ("Recvfrom: %d\n", result);
  229.         DEBUGBYTES (buf, result);
  230.         DEBUG0 ("\n");
  231.  
  232.         udp = udpNew (s, sin->sin_addr.s_addr, sin->sin_port);
  233.         smp = smpNew (udp, udpSend, myUpCall);
  234.  
  235.         for (bp = buf; ((result > 0) &&
  236.             (smpInput (smp, *bp++) == errOk));
  237.             result--);
  238.  
  239.         smp = smpFree (smp);
  240.         udp = udpFree (udp);
  241.  
  242.     } while (result >= 0);
  243.  
  244.     (void) perror ("recv");
  245.     communityId = apsFree (communityId);
  246.     return (close (s));
  247. }
  248.  
  249. int    main (argc, argv)
  250.  
  251. int    argc;
  252. char    *argv [];
  253.  
  254. {
  255.     exit (snmpdCommand (argc, argv));
  256. }
  257.  
  258.